package com.dh.lc.dao;

import java.util.List;

import org.junit.Test;

import com.dh.lc.mo.Coder;
import com.dh.lc.mo.ContactInfo;
import com.dh.lc.mo.Project;
import com.google.appengine.repackaged.org.apache.commons.logging.Log;
import com.google.appengine.repackaged.org.apache.commons.logging.LogFactory;

public class CoderDAOTestCase extends LocalDatastoreTestCase {
	
	private final static Log LOG = LogFactory.getLog(CoderDAOTestCase.class);
	
	@Test
    public void testAddCoder() {
		// two coder object's values.
		String coder1NickName = "coderA";
		String coder2NickName = "coderB";
		String coder1Email = "coderA@lc.com";
		String coder2Email = "coderB@lc.com";
		
		// two coder objects
        Coder coder1 = new Coder();
        coder1.setNickName(coder1NickName);
        Coder coder2 = new Coder();
        coder2.setNickName(coder2NickName);
        LOG.debug(coder1.getId() + "  " + coder2.getId());
        
        // coder's contactInfo object
        ContactInfo info1 = new ContactInfo();
        info1.setEmail(coder1Email);
        ContactInfo info2 = new ContactInfo();
        info2.setEmail(coder2Email);
        
        // set coder's sub-field contactInfo.
        coder1.setContactInfo(info1);
        coder2.setContactInfo(info2);

        // save the coders
        CoderDAO dao = new CoderDAO();
        dao.addCoder(coder1);
        dao.addCoder(coder2);
        
        System.out.println(coder1.getId() + "  " + coder2.getId());
        
        // get the amount of the coder.
        long amount = dao.getCoderAmount();
        LOG.debug("coder amount: " + amount);
        assertTrue(amount == 2);
        
        
        // get the coder from DB.
        Coder coder1Data = dao.getCoder(coder1.getId());
        Coder coder2Data = dao.getCoder(coder2.getId());
        
        assertTrue(coder1.getId().equals(coder1Data.getId()));
        
        assertTrue(coder1NickName.equals(coder1Data.getNickName()));
        assertTrue(coder1Email.equals(coder1Data.getContactInfo().getEmail()));
        
        assertTrue(coder2NickName.equals(coder2.getNickName()));
        assertTrue(coder2Email.equals(coder2Data.getContactInfo().getEmail()));
        
        
        Coder tmpCoder = dao.getCoderByEmail(coder1Email);
        System.out.println("getCoder by email: " + tmpCoder.getNickName() + tmpCoder.getContactInfo().getEmail());
        
        
        
        // add projects
        String prj1AName = "prj A for 1";
        String prj1BName = "prj B for 1";
        String prj1CName = "prj C for 1";
        Project prj1_A =  new Project(prj1AName);
        Project prj1_B =  new Project(prj1BName);
        Project prj1_C =  new Project(prj1CName);
        
        List<Project> coder1sPrjects = coder1Data.getProjects();
        coder1sPrjects.add(prj1_A);
        coder1sPrjects.add(prj1_B);
        coder1sPrjects.add(prj1_C);
        coder1Data.setProjects(coder1sPrjects);
        dao.updateCoder(coder1Data);
        
        Coder coder1Data1 = dao.getCoder(coder1.getId());
        assertTrue(coder1Data1.getProjects().size() == 3);
        for (Project p : coder1Data1.getProjects()) {
        	System.out.println(p.getName());
        }
    }
	


    public void testDelete() {

    }
}